package com.icomvn.game.pikchu.solution;

import com.icomvn.game.pikachu.play.PlayPikachu;

public class Solution {
	public static int lengthX = 0;
	public static int lengthY = 0;

	public static boolean increateX(int x, int y, int xIncreate, int turn,
			int endX, int endY, String road, int result[][],int step) {
		if(step>PlayPikachu.cost)return false;
		if (x + 1 == endX && y == endY) {
			int turn1 = 0;
			if (xIncreate == 0)
				turn1 = 0;
			else if (xIncreate == 1 || xIncreate == -1)
				turn1 = turn;
			else
				turn1 = turn + 1;
			if (turn1 > 2)
				return false;
			else {
				PlayPikachu.road = road;
				//Log.e("print", "turn=" + String.valueOf(turn));
				return true;
			}
		}
		road = road + "<x>" + String.valueOf(x + 1) + "</x><y>"
				+ String.valueOf(y) + "</y>";
		// Log
		// .e("print", "x=" + String.valueOf(x + 1) + ";y="
		// + String.valueOf(y));
		if (result[x + 1][y] == 0) {
			int turn1 = 0;
			if (xIncreate == 0)
				turn1 = 0;
			else if (xIncreate == 1 || xIncreate == -1)
				turn1 = turn;
			else
				turn1 = turn + 1;
			if (turn1 > 2)
				return false;
			else if (checkRoad(x + 1, y, 1, turn1, endX, endY, road, result,
					lengthX, lengthY,step))
				return true;
		}
		return false;
	}

	public static boolean increateY(int x, int y, int xIncreate, int turn,
			int endX, int endY, String road, int result[][],int step) {
		if(step>PlayPikachu.cost)return false;
		if (x == endX && y + 1 == endY) {
			int turn1 = 0;
			if (xIncreate == 0)
				turn1 = 0;
			else if (xIncreate == 2 || xIncreate == -2)
				turn1 = turn;
			else
				turn1 = turn + 1;
			if (turn1 > 2)
				return false;
			else {
				PlayPikachu.road = road;
				//Log.e("print", "turn=" + String.valueOf(turn));
				return true;
			}
		}
		road = road + "<x>" + String.valueOf(x) + "</x><y>"
				+ String.valueOf(y + 1) + "</y>";
		// Log
		// .e("print", "x=" + String.valueOf(x) + ";y="
		// + String.valueOf(y + 1));
		if (result[x][y + 1] == 0) {
			int turn1 = 0;
			if (xIncreate == 0)
				turn1 = 0;
			else if (xIncreate == 2 || xIncreate == -2)
				turn1 = turn;
			else
				turn1 = turn + 1;
			if (turn1 > 2)
				return false;
			else if (checkRoad(x, y + 1, 2, turn1, endX, endY, road, result,
					lengthX, lengthY,step))
				return true;
		}
		return false;
	}

	public static boolean decreateX(int x, int y, int xIncreate, int turn,
			int endX, int endY, String road, int result[][],int step) {
		if(step>PlayPikachu.cost)return false;
		if (x - 1 == endX && y == endY) {
			int turn1 = 0;
			if (xIncreate == 0)
				turn1 = 0;
			else if (xIncreate == 1 || xIncreate == -1)
				turn1 = turn;
			else
				turn1 = turn + 1;
			if (turn1 > 2)
				return false;
			else {
				PlayPikachu.road = road;
				//Log.e("print", "turn=" + String.valueOf(turn));
				return true;
			}
		}
		road = road + "<x>" + String.valueOf(x - 1) + "</x><y>"
				+ String.valueOf(y) + "</y>";
		// Log.e("print", "x=" + String.valueOf(x - 1) + ";y="
		// + String.valueOf(y - 1));
		if (result[x - 1][y] == 0) {
			int turn1 = 0;
			if (xIncreate == 0)
				turn1 = 0;
			else if (xIncreate == 1 || xIncreate == -1)
				turn1 = turn;
			else
				turn1 = turn + 1;
			if (turn1 > 2)
				return false;
			else if (checkRoad(x - 1, y, -1, turn1, endX, endY, road, result,
					lengthX, lengthY,step))
				return true;
		}
		return false;
	}

	public static boolean decreateY(int x, int y, int xIncreate, int turn,
			int endX, int endY, String road, int result[][],int step) {
		if(step>PlayPikachu.cost)return false;
		if (x == endX && y - 1 == endY) {
			int turn1 = 0;
			if (xIncreate == 0)
				turn1 = 0;
			else if (xIncreate == 2 || xIncreate == -2)
				turn1 = turn;
			else
				turn1 = turn + 1;
			if (turn1 > 2)
				return false;
			else {
				PlayPikachu.road = road;
				//Log.e("print", "turn=" + String.valueOf(turn));
				return true;
			}
		}
		road = road + "<x>" + String.valueOf(x) + "</x><y>"
				+ String.valueOf(y - 1) + "</y>";
		// Log
		// .e("print", "x=" + String.valueOf(x) + ";y="
		// + String.valueOf(y - 1));
		if (result[x][y - 1] == 0) {
			int turn1 = 0;
			if (xIncreate == 0)
				turn1 = 0;
			else if (xIncreate == 2 || xIncreate == -2)
				turn1 = turn;
			else
				turn1 = turn + 1;
			if (turn1 > 2)
				return false;
			else if (checkRoad(x, y - 1, -2, turn1, endX, endY, road, result,
					lengthX, lengthY,step))
				return true;
		}
		return false;
	}

	public static boolean checkRoad(int x, int y, int xIncreate, int turn,
			int endX, int endY, String road, int result[][], int lengthX,
			int lengthY, int step) {
		Solution.lengthX = lengthX;
		Solution.lengthY = lengthY;
		step++;
		if (x > 0) {
			if (y > 0) {
				if (x < lengthX - 1) {// x ở giữa
					if (y < lengthY - 1) {
						if (!(xIncreate == -1))
							if (increateX(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
						if (!(xIncreate == 1))
							if (decreateX(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
						if (!(xIncreate == -2))
							if (increateY(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
						if (!(xIncreate == 2))
							if (decreateY(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
					} else {// x ở giữa y ở biên
						// tăng x
						if (!(xIncreate == -1))
							if (increateX(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
						// giảm x
						if (!(xIncreate == 1))
							if (decreateX(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
						// giảm y
						if (!(xIncreate == 2))
							if (decreateY(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
					}
				} else {// x ở biên
					if (y < lengthY - 1) {// y ở giữa
						if (!(xIncreate == 1))
							if (decreateX(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
						if (!(xIncreate == -2))
							if (increateY(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
						if (!(xIncreate == 2))
							if (decreateY(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
					} else {// x ở biên y ở biên
						// giảm x
						if (!(xIncreate == 1))
							if (decreateX(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
						// giảm y
						if (!(xIncreate == 2))
							if (decreateY(x, y, xIncreate, turn, endX, endY,
									road, result, step))
								return true;
					}
				}
			} else {// y=0,x>0
				if (x < lengthX - 1) {
					// y tăng x tưng hoặc giảm
					// y tăng
					if (!(xIncreate == -2))
						if (increateY(x, y, xIncreate, turn, endX, endY, road,
								result, step))
							return true;
					// x tăng
					if (!(xIncreate == -1))
						if (increateX(x, y, xIncreate, turn, endX, endY, road,
								result, step))
							return true;
					// x giamr
					if (!(xIncreate == 1))
						if (decreateX(x, y, xIncreate, turn, endX, endY, road,
								result, step))
							return true;
				} else {
					// y tăng,x giảm
					// y tăng
					if (!(xIncreate == -2))
						if (increateY(x, y, xIncreate, turn, endX, endY, road,
								result, step))
							return true;
					// x giảm
					if (!(xIncreate == 1))
						if (decreateX(x, y, xIncreate, turn, endX, endY, road,
								result, step))
							return true;
				}
			}
		} else {
			if (y > 0) {
				if (y < lengthY - 1) {
					// tăng x
					if (!(xIncreate == -1))
						if (increateX(x, y, xIncreate, turn, endX, endY, road,
								result, step))
							return true;
					// tăng y
					if (!(xIncreate == -2))
						if (increateY(x, y, xIncreate, turn, endX, endY, road,
								result, step))
							return true;
					// giảm y
					if (!(xIncreate == 2))
						if (decreateY(x, y, xIncreate, turn, endX, endY, road,
								result, step))
							return true;
				} else {
					// tăng x
					if (!(xIncreate == -1))
						if (increateX(x, y, xIncreate, turn, endX, endY, road,
								result, step))
							return true;
					// giảm y
					if (!(xIncreate == 2))
						if (decreateY(x, y, xIncreate, turn, endX, endY, road,
								result, step))
							return true;
				}
			} else {
				if (!(xIncreate == -1))
					if (increateX(x, y, xIncreate, turn, endX, endY, road,
							result, step))
						return true;
				if (!(xIncreate == -2))
					if (increateY(x, y, xIncreate, turn, endX, endY, road,
							result, step))
						return true;
			}
		}
		return false;
	}

}
